package com.yanze.schoolinformation.view.information.movie;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;

import com.yanze.schoolinformation.R;
import com.yanze.schoolinformation.adapter.rv.MovieDetailAdapter;
import com.yanze.schoolinformation.base.BaseHeaderActivity;
import com.yanze.schoolinformation.databinding.ActivityMovieDetailBinding;
import com.yanze.schoolinformation.databinding.HeaderSlideShapeBinding;
import com.yanze.schoolinformation.app.C;
import com.yanze.schoolinformation.model.bean.MovieDetailBean;
import com.yanze.schoolinformation.model.bean.moviechild.SubjectsBean;
import com.yanze.schoolinformation.model.http.DeforObserver;
import com.yanze.schoolinformation.model.http.cache.ACache;
import com.yanze.schoolinformation.model.http.reqo.DataReqo;
import com.yanze.schoolinformation.utils.CommonUtils;
import com.yanze.schoolinformation.utils.StringFormatUtil;
import com.yanze.schoolinformation.utils.ToastUtils;
import com.yanze.schoolinformation.view.webview.WebViewActivity;

import io.reactivex.disposables.Disposable;

public class MovieDetailActivity extends BaseHeaderActivity<HeaderSlideShapeBinding, ActivityMovieDetailBinding> {

    private SubjectsBean subjectsBean;
    private String mMoreUrl;
    private String mMovieName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_movie_detail);
        if (getIntent() != null) {
            subjectsBean = (SubjectsBean) getIntent().getSerializableExtra("bean");
        }

        initSlideShapeTheme(setHeaderImgUrl(), setHeaderImageView());

        setTitle(subjectsBean.getTitle());
        setSubTitle(String.format("主演：%s", StringFormatUtil.formatName(subjectsBean.getCasts())));
//        ImgLoadUtil.showImg(bindingHeaderView.ivOnePhoto,subjectsBean.getImages().getLarge());
        bindingHeaderView.setSubjectsBean(subjectsBean);
        bindingHeaderView.executePendingBindings();

        loadMovieDetail();
    }

    private void loadMovieDetail() {
        DataReqo.getInstant().getMovieDetail(C.url.API_DOUBAN, subjectsBean.getId())
                .subscribe(new DeforObserver<MovieDetailBean>() {
                    @Override
                    public void _onNext(MovieDetailBean entity) {
                        // 上映日期
                        bindingHeaderView.tvOneDay.setText(String.format("上映日期：%s", entity.getYear()));
                        // 制片国家
                        bindingHeaderView.tvOneCity.setText(String.format("制片国家/地区：%s", StringFormatUtil.formatGenres(entity.getCountries())));
                        bindingHeaderView.setMovieDetailBean(entity);
                        bindingContentView.setBean(entity);
                        bindingContentView.executePendingBindings();

                        mMoreUrl = entity.getAlt();
                        mMovieName = entity.getTitle();
                        transformData(entity);
                    }

                    @Override
                    public void _onError(String msg) {
                        ToastUtils.show(MovieDetailActivity.this, msg);
                        showError();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        showContentView();
                    }

                    @Override
                    public void _onSubscribe(Disposable disposable) {
                        addDisposable(disposable);
                    }
                });
    }

    //改造数据
    private void transformData(final MovieDetailBean movieDetailBean) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < movieDetailBean.getDirectors().size(); i++) {
                    movieDetailBean.getDirectors().get(i).setType("导演");
                }
                for (int i = 0; i < movieDetailBean.getCasts().size(); i++) {
                    movieDetailBean.getCasts().get(i).setType("演员");
                }

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        setAdapter(movieDetailBean);
                    }
                });
            }
        }).start();
    }

    private void setAdapter(MovieDetailBean personBean) {
        bindingContentView.xrvCast.setVisibility(View.VISIBLE);
        bindingContentView.xrvCast.setLayoutManager(new LinearLayoutManager(MovieDetailActivity.this));
        bindingContentView.xrvCast.setPullRefreshEnabled(false);
        bindingContentView.xrvCast.setLoadingMoreEnabled(false);

        //需要加 不然滑动不流畅
        bindingContentView.xrvCast.setNestedScrollingEnabled(false);
        bindingContentView.xrvCast.setHasFixedSize(false);

        MovieDetailAdapter mAdapter = new MovieDetailAdapter();
        mAdapter.addAll(personBean.getDirectors());
        mAdapter.addAll(personBean.getCasts());
        bindingContentView.xrvCast.setAdapter(mAdapter);
    }

    @Override
    protected int setHeaderLayout() {
        return R.layout.header_slide_shape;
    }

    @Override
    protected String setHeaderImgUrl() {
        if (subjectsBean == null) {
            return "";
        }
        return subjectsBean.getImages().getMedium();
    }

    @Override
    protected ImageView setHeaderImageView() {
        return bindingHeaderView.imgItemBg;
    }

    @Override
    protected void setTitleClickMore() {
        WebViewActivity.loadUrl(MovieDetailActivity.this, mMoreUrl, mMovieName);
    }

    @Override
    protected void onRefresh() {
        loadMovieDetail();
    }

    /**
     * 界面入口
     *
     * @param context      activity
     * @param positionData bean
     * @param imageView    imageView
     */
    public static void start(Activity context, SubjectsBean positionData, ImageView imageView) {
        Intent intent = new Intent(context, MovieDetailActivity.class);
        intent.putExtra("bean", positionData);
        ActivityOptionsCompat options =
                ActivityOptionsCompat.makeSceneTransitionAnimation(context,
                        imageView, CommonUtils.getString(R.string.transition_movie_img));//与xml文件对应
        ActivityCompat.startActivity(context, intent, options.toBundle());
    }
}
